package com.open.exam.config;

import com.open.exam.properties.JedisProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @Description reid主从配置
 * @author 奶茶
 * Created by on 2018/10/23 18:35.
 */
@Configuration
@EnableConfigurationProperties(value = JedisProperties.class)
public class RedisSentinelConfig extends CachingConfigurerSupport {

    private static final Logger logger = LoggerFactory.getLogger(RedisSentinelConfig.class);

    @Autowired
    private JedisProperties jedisProperties;


    /**
     * redis 哨兵配置
     * @return
     */
    @Bean
    public RedisSentinelConfiguration redisSentinelConfiguration(){
        RedisSentinelConfiguration redisSentinelConfiguration = new RedisSentinelConfiguration();
        String[] redisNodes = jedisProperties.getRedisNodes().split(",");
        for (String node:redisNodes) {
            String[] item = node.split(":");
            String ip = item[0];
            String port = item[1];
            redisSentinelConfiguration.addSentinel(new RedisNode(ip,Integer.valueOf(port)));
        }
        redisSentinelConfiguration.setDatabase(jedisProperties.getDatabase());
        redisSentinelConfiguration.setPassword(RedisPassword.of(jedisProperties.getPassword()));
        redisSentinelConfiguration.setMaster(jedisProperties.getMaster());
        return redisSentinelConfiguration;
    }

    /**
     * redis 连接工厂类
     * @return
     */
    @Bean
    public JedisConnectionFactory jedisConnectionFactory(){
        logger.info("-------------------初始redis化链接工厂--------------------");
        JedisClientConfiguration.JedisClientConfigurationBuilder builder = JedisClientConfiguration.builder();
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        JedisProperties.Pool pool = jedisProperties.getJedis().getPool();
        jedisPoolConfig.setMaxIdle(pool.getMaxIdle());
        jedisPoolConfig.setMinIdle(pool.getMinIdle());
        jedisPoolConfig.setMaxWaitMillis(pool.getMaxWait());
        jedisPoolConfig.setMaxTotal(pool.getMaxActive());
        return new JedisConnectionFactory(redisSentinelConfiguration(),builder.build());
    }

    /**
     * 配置redisTemplate
     * @return
     */
    @Bean
    public RedisTemplate<Object,Object> redisTemplate(){
        //redisTemplate的构造方法默认设置了stringSerializer
        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
        //开启事物
        redisTemplate.setEnableTransactionSupport(false);
        //set key serializer
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        //key 序列化方式
        redisTemplate.setKeySerializer(stringRedisSerializer);
        //value 序列化方式
        redisTemplate.setValueSerializer(stringRedisSerializer);
        //value hashmap序列化
        redisTemplate.setHashValueSerializer(stringRedisSerializer);
        redisTemplate.setStringSerializer(stringRedisSerializer);
        //key haspmap序列化
        redisTemplate.setHashKeySerializer(stringRedisSerializer);
        redisTemplate.setConnectionFactory(jedisConnectionFactory());
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
}
